package com.thinkgem.dingdan.modules.bu.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.dingdan.common.persistence.Page;
import com.thinkgem.dingdan.common.service.CrudService;
import com.thinkgem.dingdan.common.utils.StringUtils;
import com.thinkgem.dingdan.modules.bu.constant.BusinessConstant;
import com.thinkgem.dingdan.modules.bu.dao.BuOrderDao;
import com.thinkgem.dingdan.modules.bu.entity.BuOrder;
import com.thinkgem.dingdan.modules.bu.entity.TotalEntity;
import com.thinkgem.dingdan.modules.cms.entity.Site;
import com.thinkgem.dingdan.modules.sys.entity.Office;
import com.thinkgem.dingdan.modules.sys.entity.User;
import com.thinkgem.dingdan.modules.sys.utils.UserUtils;

@Service
@Transactional(readOnly = true)
public class BuOrderService extends CrudService<BuOrderDao, BuOrder> implements IBuOrderService,IBuExternalOrderService{

	@Resource
	private BuOrderDao buOrderDao;
	
	public BuOrder get(String id) {
		return super.get(id);
	}
	
	@Override
	public BuOrder get(BuOrder buOrder){
		/*User user = UserUtils.getUser();
		buOrder.setOffice(new Office());
		buOrder.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buOrder.setSite(new Site());*/
		return buOrderDao.get(buOrder);
	}
	
	public List<BuOrder> findList(BuOrder buOrder) {
		return super.findList(buOrder);
	}
	
	public Page<BuOrder> findByManagePage(Page<BuOrder> page, BuOrder buOrder) {
		User user = UserUtils.getUser();
		buOrder.setOffice(new Office());
		buOrder.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buOrder.setSite(new Site());
		return super.findPage(page, buOrder);
	}
	
	public Page<BuOrder> findPage(Page<BuOrder> page, BuOrder buOrder) {
		User user = UserUtils.getUser();
		buOrder.setOffice(new Office());
		buOrder.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buOrder.setSite(new Site());
		return super.findPage(page, buOrder);
	}
	
	public Page<BuOrder> findByGenDanPage(Page<BuOrder> page, BuOrder buOrder) {
		User user = UserUtils.getUser();
		buOrder.setOffice(new Office());
		buOrder.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buOrder.setSite(new Site());
		return super.findByGenDanPage(page, buOrder);
	}
	
	public Page<BuOrder> findManageExportPage(Page<BuOrder> page, BuOrder buOrder) {
		User user = UserUtils.getUser();
		buOrder.setOffice(new Office());
		buOrder.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buOrder.setSite(new Site());
		return super.findManagePage(page, buOrder);
	}
	
	public Page<BuOrder> findCreateByExportPage(Page<BuOrder> page, BuOrder buOrder) {
		User user = UserUtils.getUser();
		buOrder.setOffice(new Office());
		buOrder.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buOrder.setSite(new Site());
		return super.findCreateByPage(page, buOrder);
	}
	
	public Page<BuOrder> findExportPage(Page<BuOrder> page, BuOrder buOrder) {
		User user = UserUtils.getUser();
		buOrder.setOffice(new Office());
		buOrder.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buOrder.setSite(new Site());
		return super.findExportPage(page, buOrder);
	}
	
	@Transactional(readOnly = false)
	public void save(BuOrder buOrder) {
		super.save(buOrder);
	}
	
	@Transactional(readOnly = false)
	public void delete(BuOrder buOrder) {
		super.delete(buOrder);
	}

	@Override
	public Page<BuOrder> findCustomerPage(Page<BuOrder> page, BuOrder buOrder) {
		return super.findCustomerPage(page, buOrder);
	}
	
	@Override
	public Page<BuOrder> findCustomerExportPage(Page<BuOrder>page,BuOrder buOrder){
		return super.findCustomerExportPage(page, buOrder);
	}

	@Override
	public Page<BuOrder> findSupplierPage(Page<BuOrder> page, BuOrder buOrder) {
		return super.findSupplierPage(page, buOrder);
	}

	@Override
	public int selectOrderStatus(BuOrder buOrder) {
		return buOrderDao.selectOrderStatus(buOrder);
	}

	@Transactional(readOnly = false)
	@Override
	public int updateDelivery(BuOrder buOrder) {
		return buOrderDao.updateDelivery(buOrder);
	}

	@Override
	public BuOrder findEntity(BuOrder buOrder) {
		return buOrderDao.findEntity(buOrder);
	}

	@Transactional(readOnly = false)
	@Override
	public int updateBatchReceipt(BuOrder buOrder) {
		return buOrderDao.updateBatchReceipt(buOrder);
	}

	@Override
	public Page<BuOrder> findDownloadSupplierPage(Page<BuOrder> page, BuOrder buOrder) {
		return super.findDownloadSupplierPage(page, buOrder);
	}

	@Override
	public TotalEntity findCreateById(BuOrder buOrder) {
		return buOrderDao.findCreateById(buOrder);
	}

	@Override
	public TotalEntity findCustomerById(BuOrder buOrder) {
		return buOrderDao.findCustomerById(buOrder);
	}

	@Override
	public TotalEntity findSupplierById(BuOrder buOrder) {
		return buOrderDao.findSupplierById(buOrder);
	}

	@Override
	public TotalEntity findAllTotal(BuOrder buOrder) {
		return buOrderDao.findAllTotal(buOrder);
	}

	@Override
	public List<BuOrder> selectCustomerIdListByCreateId(BuOrder buOrder) {
		return buOrderDao.selectCustomerIdListByCreateId(buOrder);
	}

	@Override
	public List<BuOrder> selectCustomerIdListForManage(BuOrder buOrder) {
		return buOrderDao.selectCustomerIdListForManage(buOrder);
	}

	@Override
	public List<BuOrder> selectCustomerIdListBySupplierId(BuOrder buOrder) {
		return buOrderDao.selectCustomerIdListBySupplierId(buOrder);
	}

	@Override
	public List<BuOrder> selectSupplierListByCreateId(BuOrder buOrder) {
		return buOrderDao.selectSupplierListByCreateId(buOrder);
	}

	@Override
	public List<BuOrder> selectSupplierIdListForManage(BuOrder buOrder) {
		return buOrderDao.selectSupplierIdListForManage(buOrder);
	}

	@Override
	public List<BuOrder> selectCustomerIdListByCustomerId(BuOrder buOrder) {
		return buOrderDao.selectCustomerIdListByCustomerId(buOrder);
	}

	@Override
	public List<BuOrder> selectSupplierListByCustomerId(BuOrder buOrder) {
		return buOrderDao.selectSupplierListByCustomerId(buOrder);
	}

	@Override
	public List<BuOrder> selectSupplierListBySupplierId(BuOrder buOrder) {
		return buOrderDao.selectSupplierListBySupplierId(buOrder);
	}

	@Override
	public List<BuOrder> findImageList() {
		return buOrderDao.findImageList();
	}

	@Transactional(readOnly = false)
	@Override
	public void updateImageUrl(String id, String picUrl) {
		buOrderDao.updateImageUrl(id, picUrl);
		
	}

	@Override
	public Page<BuOrder> findSupplierExportPage(Page<BuOrder> page, BuOrder buOrder) {
		return super.findSupplierExportPage(page, buOrder);
	}

	@Override
	public BuOrder findById(BuOrder buOrder) {
		return buOrderDao.findById(buOrder);
	}

	@Override
	public TotalEntity total(BuOrder buOrder,User user) {
		
		TotalEntity entity = new TotalEntity();
		
		BuOrder buCu = new BuOrder();
		buCu.setCustomerId(user.getId());
		buCu.setDelFlag(BusinessConstant.NOT_DEL_FLAG);
		buCu.setOrderType(BusinessConstant.OUT_ORDER_TYPE);

		if(org.apache.commons.lang3.StringUtils.isNotBlank(buOrder.getSalesmanId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSalesmanId())){
			buCu.setSalesmanId(buOrder.getSalesmanId());
		}
		if(StringUtils.isNotBlank(buOrder.getSupplierId()) && !(BusinessConstant.SEL_ORDER_STATE_FLAG).equals(buOrder.getSupplierId())){
			buCu.setSupplierId(buOrder.getSupplierId());
		}

		buCu.setOrderState(buOrder.getOrderState());
		buCu.setProductName(buOrder.getProductName());
		buCu.setShipDate(buOrder.getShipDate());
		buCu.setBeginOrderDate(buOrder.getBeginOrderDate());
		buCu.setEndOrderDate(buOrder.getEndOrderDate());
        buCu.setBeginShipDate(buOrder.getBeginShipDate());
		buCu.setEndShipDate(buOrder.getEndShipDate());
		buCu.setProductNo(buOrder.getProductNo());
		buCu.setOrderNo(buOrder.getOrderNo());

		entity = buOrderDao.findCustomerById(buCu);
		return entity;
	}

}